package com.example.mediademo.controller;

import com.example.mediademo.dto.MediaDTO;
import com.example.mediademo.dto.MediaTypeDTO;
import com.example.mediademo.entity.Media;
import com.example.mediademo.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api")
public class MediaController {
    @Autowired
    private MediaService mediaService;

    // 单个编号查询
    @GetMapping("/media")
    public MediaDTO getMediaDetail(@RequestParam String keynumber) {
        return mediaService.getMediaDetails(keynumber);
    }

    // 多个编号查询
    @GetMapping("/medias")
    public List<MediaDTO> getMediaDetails(@RequestParam String keynumbers) {
        return mediaService.getMediaDetailsByKeynumbers(keynumbers);
    }

    // 按类型分页查询
    @GetMapping("/media/type")
    public Page<Media> getByType(
        @RequestParam int type,
        @RequestParam(defaultValue = "0") int page,
        @RequestParam(defaultValue = "10") int size
    ) {
        return mediaService.findByType(type, PageRequest.of(page, size));
    }

    // 获取所有媒体类型
    @GetMapping("/types")
    public List<MediaTypeDTO> getAllMediaTypes() {
        return mediaService.getAllMediaTypes();
    }

    // 分页查询所有媒体
    @GetMapping("/media/list")
    public Page<MediaDTO> getAllMedia(
        @RequestParam(defaultValue = "0") int page,
        @RequestParam(defaultValue = "10") int size
    ) {
        return mediaService.findAllMedia(PageRequest.of(page, size));
    }

    // 条件查询
    @GetMapping("/media/search")
    public Page<MediaDTO> searchMedia(
        @RequestParam(required = false) Integer type,
        @RequestParam(required = false) String keynumbers,
        @RequestParam(defaultValue = "0") int page,
        @RequestParam(defaultValue = "10") int size
    ) {
        return mediaService.findByConditions(type, keynumbers, PageRequest.of(page, size));
    }
}
